package com.example.service.ipml;
import com.alibaba.fastjson.JSON;
import com.example.mapper.DivisionMapper;
import com.example.mapper.DoctorMapper;
import com.example.mapper.RegistrationMapper;
import com.example.mapper.SickMapper;
import com.example.pojo.dto.RegistrationDTO;
import com.example.pojo.dto.SickDTO;
import com.example.pojo.entitly.Registration;
import com.example.pojo.entitly.Sick;
import com.example.pojo.vo.DivisionStandardVO;
import com.example.pojo.vo.DoctorStandardVO;
import com.example.pojo.vo.RegistrationVO;
import com.example.service.IRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class RegistrationServiceIpml implements IRegistrationService {
    public RegistrationServiceIpml() {
        log.debug("创建挂号业务类对象RegistrationServiceImpl");
    }

    @Autowired
    SickMapper sickMapper;

    @Autowired
    RegistrationMapper registrationMapper;

    @Autowired
    DivisionMapper divisionMapper;

    @Autowired
    DoctorMapper doctorMapper;

//   TODO 查询是否有号h

    @Override
    public void registrationInsert(Map<String, Object> data) {
        log.debug("开始处理挂号业务参数为:{}",data);
        SickDTO sickDTO = JSON.parseObject(data.get("user").toString(), SickDTO.class);
        RegistrationDTO registrationDTO = JSON.parseObject(data.get("registration").toString(), RegistrationDTO.class);
        int count = sickMapper.selectSickByIdentity(sickDTO.getIdentity());
        if (count == 0) {
            log.debug("插入病人信息到病人表");
            Sick sick=new Sick();
            BeanUtils.copyProperties(sickDTO,sick);
            sickMapper.insertSelective(sick);
        }
        Registration registration = new Registration();
        BeanUtils.copyProperties(registrationDTO, registration);
//        根据提交过来的科室名查询科室id
        DivisionStandardVO divisionStandardVO = divisionMapper.selectByOffice(registrationDTO.getOffice());
//        根据提交过来的医生名查询医生id
        DoctorStandardVO doctorStandardVO = doctorMapper.selectByName(registrationDTO.getDocterName());
//        根据医生id修改最大限制人数
        log.debug("根据医生id修改最大限制人数参数为：{}",divisionStandardVO.getId());
        doctorMapper.updateDoctorById(doctorStandardVO.getId());
        registration.setPatientId(sickDTO.getUserId());
        registration.setDepartmentId(divisionStandardVO.getId());
        registration.setDoctorId(doctorStandardVO.getId());
        log.debug("正在插入挂号信息:{}",registration);
        registrationMapper.insert(registration);
    }

    @Override
    public void deleteSick(Long id){
        log.debug("正在处理删除病人信息业务参数为：{}",id);
        sickMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void registrationDeleteById(Long id){
        log.debug("正在处理删除挂号信息业务参数为：{}",id);
        registrationMapper.deleteByPrimaryKey(id);
    }

}
